package com.example.administrator.footballgamecollectandanalyze;


import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity {

    private String username;
    private Button myteam;
    private ViewPager mViewpage;
    private RadioGroup radioGroup;
    private Button CreateTeam;
    private Button CreateGame;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myteam=(Button) findViewById(R.id.MyTeam);
        Intent intent = getIntent();
        username =intent.getStringExtra("username");
        intiViewpager();
        intiRedio();

        myteam.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               startActivity(new Intent(MainActivity.this, MyTeam.class));
            }
        });
    }

    private void intiRedio() {
        // TODO Auto-generated method stub
        radioGroup = (RadioGroup)findViewById(R.id.radioGroup1);
        final RadioButton radio0 = (RadioButton)findViewById(R.id.radio0);
        final RadioButton radio1 = (RadioButton)findViewById(R.id.radio1);
//        final RadioButton radio2 = (RadioButton)findViewById(R.id.radio2);

        mViewpage.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int id) {
                // TODO Auto-generated method stub
                switch (id) {
                    case 0:
                        radio0.setChecked(true);
                        break;
                    case 1:
                        radio1.setChecked(true);
                        break;
//                    case 2:
//                        radio2.setChecked(true);
//                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup arg0, int id) {
                // TODO Auto-generated method stub
                switch (id) {
                    case R.id.radio0:
                        mViewpage.setCurrentItem(0);
                        break;
                    case R.id.radio1:
                        mViewpage.setCurrentItem(1);
                        break;
//                    case R.id.radio2:
//                        mViewpage.setCurrentItem(2);
//                        break;
                    default:
                        break;
                }
            }
        });
    }

    private void intiViewpager() {
        // TODO Auto-generated method stub
        LayoutInflater inflater = getLayoutInflater().from(this);
        View view1 = inflater.inflate(R.layout.activity_mygame,null);
        View view2 = inflater.inflate(R.layout.activity_order_game,null);
//        View view3 = inflater.inflate(R.layout.activity_order_game,null);

        CreateTeam = (Button) view2.findViewById(R.id.CreatTeam1);
        CreateTeam.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, CreateTeam.class));
            }
        });
        CreateGame = (Button) view2.findViewById(R.id.CreateGame);

        CreateGame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, CreateGame.class));
            }
        });
        final List<View> ViewList = new ArrayList<View>();

        ViewList.add(view1);
        ViewList.add(view2);
//        ViewList.add(view3);

        mViewpage = (ViewPager)findViewById(R.id.ViewPager);

        PagerAdapter pagerAdapter = new PagerAdapter() {

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                // TODO Auto-generated method stub
                container.addView(ViewList.get(position));
                return ViewList.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                // TODO Auto-generated method stub
                container.removeView(ViewList.get(position));
            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                // TODO Auto-generated method stub

                return arg0==arg1;
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return ViewList.size();
            }
        };
        mViewpage.setAdapter(pagerAdapter);
    }
}
